package com.sy.wz.demo.controller;

import com.sy.core.exception.BusinessException;
import com.sy.wz.common.Result;
import com.sy.wz.demo.model.UserPo;
import com.sy.wz.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Create by wongloong on 17-8-17
 */
@Api(value = "用户前置api")
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation(value = "新增用户", notes = "新增用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "UserPo")
    })
    @PostMapping(value = "/add", consumes = "application/json")
    @ResponseBody
    public boolean testAdd(@RequestBody UserPo userPo) {
//        UserPo userPo = new UserPo();
//        userPo.setUsername("haha");
        System.out.println(userPo);
        return userService.insert(userPo);
    }

    @ApiOperation(value = "获取用户", notes = "获取用户信息")
    @GetMapping(value = "/list")
    @ResponseBody
    public List<UserPo> list() {
        return userService.selectByMap(null);
    }


    @ApiOperation(value = "测试事务", notes = "测试事务")
    @GetMapping(value = "/addTestTran")
    @ResponseBody
    public Result addTestTran() {
        Result result = new Result();
        try {
            boolean r = userService.addTestTran();
        } catch (BusinessException e) {
            result.setCode(0);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}
